import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../models/app_theme.dart';

part 'theme_event.dart';
part 'theme_state.dart';

class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
  ThemeBloc() 
   : super(const ThemeState(AppThemeMode.Light,AppThemeColorMode.Blue,AppFontMode.Roboto)) {
     AppTheme.appTheme.setThemeState(state.themeMode,state.themeColorMode,state.fontMode);
    on<ThemeChangeEvent>((event, emit) {
      ThemeState state=ThemeState(event.themeMode, event.themeColorMode, event.fontMode);
      AppTheme.appTheme.setThemeState(state.themeMode, state.themeColorMode, state.fontMode);
      emit(state);
    });
    on<ThemeLoadEvent>((event,emit) async {
      SharedPreferences shared=await SharedPreferences.getInstance();
      AppThemeMode appThemeMode=getInitThemeMode(shared.getString(THEME_MODE)??'Dark');
      AppThemeColorMode colorMode=getInitColorMode(shared.getString(COLOR_MODE)??'Blue');
      ThemeState state=ThemeState(appThemeMode, colorMode, AppFontMode.Roboto);
      AppTheme.appTheme.setThemeState(state.themeMode, state.themeColorMode, state.fontMode);
      emit(state);
    });
  }
}
